package com.jiajie.gsaccessingdatajpa.concurrent;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * CAS 乐观锁
 * @author jay
 */
public class TestCas {

    private static AtomicInteger count = new AtomicInteger(0);

    public static void main(String[] args) {
        for (int i = 0; i < 2; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    // 每个线程让count自增100次
                    for (int i = 0; i < 100; i++) {
//                        count++;
//                        synchronized (TestCas.class){
//                            count++;
//                        }
                        count.incrementAndGet();
                    }
                }
            }).start();
        }

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(count);
    }
}
